/*     */ package com.jcraft.jzlib;
/*     */ 
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.DataOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ZlibUtil
/*     */ {
/*     */   public static int compress(byte[] src, byte[] obj)
/*     */   {
/*  19 */     int err = 0;
/*  20 */     ZStream c_stream = new ZStream();
/*  21 */     err = c_stream.deflateInit(-1);
/*  22 */     CHECK_ERR(c_stream, err, "deflateInit");
/*     */     
/*  24 */     c_stream.next_in = src;
/*  25 */     c_stream.avail_in = src.length;
/*  26 */     c_stream.next_in_index = 0;
/*     */     
/*  28 */     c_stream.next_out = obj;
/*  29 */     c_stream.avail_out = obj.length;
/*  30 */     c_stream.next_out_index = 0;
/*     */     
/*  32 */     err = c_stream.deflate(0);
/*  33 */     CHECK_ERR(c_stream, err, "deflate");
/*     */     for (;;)
/*     */     {
/*  36 */       c_stream.avail_out = 1;
/*  37 */       err = c_stream.deflate(4);
/*  38 */       if (err == 1)
/*     */         break;
/*  40 */       CHECK_ERR(c_stream, err, "deflate");
/*     */     }
/*  42 */     if (c_stream.avail_in != 0) {
/*  43 */       System.out.println("deflate not greedy");
/*  44 */       System.exit(1);
/*     */     }
/*  46 */     err = c_stream.deflateEnd();
/*  47 */     CHECK_ERR(c_stream, err, "deflateEnd");
/*  48 */     return (int)c_stream.total_out;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static byte[] uncompress(byte[] src, int length)
/*     */   {
/*  62 */     byte[] uncomp = null;
/*  63 */     byte[] tmp = new byte[length];
/*  64 */     ZStream d_stream = new ZStream();
/*  65 */     d_stream.next_in = src;
/*  66 */     d_stream.next_in_index = 0;
/*  67 */     d_stream.avail_in = src.length;
/*     */     
/*  69 */     int err = d_stream.inflateInit();
/*  70 */     CHECK_ERR(d_stream, err, "inflateInit");
/*     */     for (;;)
/*     */     {
/*  73 */       d_stream.next_out = tmp;
/*  74 */       d_stream.next_out_index = 0;
/*  75 */       d_stream.avail_out = length;
/*  76 */       err = d_stream.inflate(0);
/*  77 */       if (err == 1)
/*     */         break;
/*  79 */       CHECK_ERR(d_stream, err, "inflate large");
/*     */     }
/*     */     
/*  82 */     err = d_stream.inflateEnd();
/*  83 */     CHECK_ERR(d_stream, err, "inflateEnd");
/*  84 */     uncomp = new byte[(int)d_stream.total_out];
/*  85 */     for (int i = 0; i < d_stream.total_out; i++) {
/*  86 */       uncomp[i] = tmp[i];
/*     */     }
/*  88 */     return uncomp;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static void CHECK_ERR(ZStream z, int err, String msg)
/*     */   {
/*  99 */     if (err != 0) {
/* 100 */       if (z.msg != null)
/* 101 */         System.out.print(z.msg + " ");
/* 102 */       System.out.println(msg + " error: " + err);
/* 103 */       System.exit(1);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static byte[] compress(byte[] object)
/*     */   {
/* 117 */     byte[] data = null;
/*     */     try {
/* 119 */       ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 120 */       ZOutputStream zOut = new ZOutputStream(out, 
/* 121 */         -1);
/* 122 */       DataOutputStream objOut = new DataOutputStream(zOut);
/* 123 */       objOut.write(object);
/* 124 */       objOut.flush();
/* 125 */       zOut.close();
/* 126 */       data = out.toByteArray();
/* 127 */       out.close();
/*     */     } catch (IOException e) {
/* 129 */       e.printStackTrace();
/*     */     }
/* 131 */     return data;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static byte[] uncompress(byte[] object)
/*     */   {
/* 144 */     byte[] data = null;
/*     */     try {
/* 146 */       ByteArrayInputStream in = new ByteArrayInputStream(object);
/* 147 */       ZInputStream zIn = new ZInputStream(in);
/* 148 */       byte[] buf = new byte['Ѐ'];
/* 149 */       int num = -1;
/* 150 */       ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 151 */       while ((num = zIn.read(buf, 0, buf.length)) != -1) {
/* 152 */         baos.write(buf, 0, num);
/*     */       }
/* 154 */       data = baos.toByteArray();
/* 155 */       baos.flush();
/* 156 */       baos.close();
/* 157 */       zIn.close();
/* 158 */       in.close();
/*     */     } catch (IOException e) {
/* 160 */       e.printStackTrace();
/*     */     }
/* 162 */     return data;
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */   {
/* 167 */     String str = "中国中国中国中国中国中国中国中国中国中国中国";
/* 168 */     byte[] original = str.getBytes();
/* 169 */     byte[] comp = new byte[100];
/* 170 */     byte[] compIn = null;
/* 171 */     byte[] uncomp = null;
/*     */     
/* 173 */     System.out.println("原始数据：");
/* 174 */     for (int i = 0; i < original.length; i++) {
/* 175 */       System.out.print(original[i] + " ");
/*     */     }
/*     */     
/* 178 */     System.out.println();
/* 179 */     System.out.println("<方法1>");
/* 180 */     long length = compress(original, comp);
/*     */     
/* 182 */     System.out.println("压缩后的数据：");
/* 183 */     for (int i = 0; i < length; i++) {
/* 184 */       System.out.print(comp[i] + " ");
/*     */     }
/*     */     
/* 187 */     compIn = new byte[(int)length];
/* 188 */     for (int i = 0; i < length; i++) {
/* 189 */       compIn[i] = comp[i];
/*     */     }
/*     */     
/* 192 */     uncomp = uncompress(compIn, 100);
/* 193 */     System.out.println("\n解压后的数据：");
/* 194 */     for (int i = 0; i < uncomp.length; i++) {
/* 195 */       System.out.print(uncomp[i] + " ");
/*     */     }
/*     */     
/* 198 */     System.out.println();
/* 199 */     System.out.println("<方法2>");
/* 200 */     byte[] jzlib = compress(original);
/*     */     
/* 202 */     System.out.println("压缩后的数据：");
/* 203 */     for (int i = 0; i < jzlib.length; i++) {
/* 204 */       System.out.print(jzlib[i] + " ");
/*     */     }
/*     */     
/* 207 */     byte[] unjzlib = uncompress(jzlib);
/* 208 */     System.out.println("\n解压后的数据：");
/* 209 */     for (int i = 0; i < unjzlib.length; i++) {
/* 210 */       System.out.print(unjzlib[i] + " ");
/*     */     }
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\com\jcraft\jzlib\ZlibUtil.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */